package com.hks360.car_treasure.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.hks360.car_treasure.R;

/**
 * Created by rwq on 2016/5/27.
 */
public class ClickToogleButton extends View implements View.OnClickListener {
    public ClickToogleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ClickToogleButton(Context context) {
        super(context);
        initView();
    }

    /**
     * 1.构造方法--实例化
     * 2.测量：onMeasure方法
     * 3.指定View的大小和位置：onLayout方法
     * 4.绘制视图：onDraw方法
     */
    private Bitmap backgroundBitmap;
    private Bitmap slideBitmap;
    private Bitmap widthSlideBitmap;
    private Bitmap heightSlideBitmap;
    private Paint paint;
    private boolean isOn = true;

    private float maxSlide;
    private OnToogleListener listener;
    /*
     * 点击事件是否生效，如果生效滑动事件无效
     * true生效
     * false无效
     */
    private boolean isClicked = false;


    private void initView() {
        paint = new Paint();
        paint.setAntiAlias(true);
        backgroundBitmap = BitmapFactory.decodeResource(getResources()
                , R.drawable.bg_clicktooglebutton_title);
        slideBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.thumb);
        widthSlideBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.right_slidepot);
        heightSlideBitmap = BitmapFactory.decodeResource(getResources(),
                R.drawable.height_pot);
        maxRight = (backgroundBitmap.getWidth() -  2 * (widthSlideBitmap.getWidth() - slideBitmap.getWidth())) - slideBitmap.getWidth();
        maxSlide = 0;
        setOnClickListener(this);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(backgroundBitmap, 0, 0, paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(3);
        paint.setTextSize(25);
        canvas.drawText("打开", widthSlideBitmap.getWidth() - slideBitmap.getWidth()  , (backgroundBitmap.getHeight() - heightSlideBitmap.getHeight()) / 2 + heightSlideBitmap.getHeight() * 3 / 4, paint);
        canvas.drawText("关闭", (widthSlideBitmap.getWidth() - slideBitmap.getWidth())*2 + maxRight + 2, (backgroundBitmap.getHeight() - heightSlideBitmap.getHeight()) / 2 + heightSlideBitmap.getHeight()  * 3 / 4, paint);
        canvas.drawBitmap(slideBitmap, widthSlideBitmap.getWidth() - slideBitmap.getWidth() + maxSlide, (backgroundBitmap.getHeight() - heightSlideBitmap.getHeight()) / 2, paint);


    }

    private float startX;
    private float lastX;

    public boolean isOn() {
        return isOn;
    }

    private int maxRight;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = startX = event.getX();
                isClicked = true;
                break;

            case MotionEvent.ACTION_MOVE:
                float newX = event.getX();
                float dx = newX - startX;
                maxSlide = maxSlide + dx;
                if (Math.abs(event.getX() - lastX) > 5) {
                    isClicked = false;
                }else {
                    return true;
                }
                startX = event.getX();
                if(maxSlide < 0){
                    maxSlide = 0;
                }
                if(maxSlide > maxRight){
                    maxSlide = maxRight;
                }
                invalidate();
                break;

            case MotionEvent.ACTION_UP:
                if (!isClicked) {
                    float newX1 = event.getX();
                    float dx1 = newX1 - lastX;
                    if (dx1 < (backgroundBitmap.getWidth() - slideBitmap.getWidth() - 2 * (widthSlideBitmap.getWidth() - slideBitmap.getWidth())) / 2) {
                        isOn = true;
                        maxSlide = 0;
                    } else {
                        isOn = false;
                        maxSlide = maxRight;
                    }
                    this.listener.onToogleChanged(isOn);
                }
                invalidate();
                break;
        }
        return true;
    }

    public void onClick(View v) {
        if(isClicked){
            isOn = !isOn;
            if (isOn) {
                maxSlide = maxRight;
            } else {
                maxSlide = 0;
            }
            invalidate();
        }
         this.listener.onToogleChanged(isOn);
    }


    public void setOnToggleListener(OnToogleListener listener) {
        this.listener = listener;
    }

    public interface OnToogleListener {
        void onToogleChanged(boolean isOpen);
    }
}
